import { SubjectComponent } from "../../../../core/subject";
import Utils from "../../../../core/utils";
import ColorPicker from "../../../some_good_prefab/assets/color_picker/color_picker";
import BulletAttrItem from "./bullet_attr_item";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BulletAttrItemColor extends BulletAttrItem {
    @property(cc.Node)
    colorNode: cc.Node = null;

    protected onLoad(): void {
        this.colorNode.on(cc.Node.EventType.TOUCH_END, async () => {
            let prefab = await Utils.loader.getAsset<cc.Prefab>("color_picker", "prefabs", "cc.Prefab");
            if (!prefab) {
                console.error("没有找到color_picker，请引入some_good_prefab包");
                return;
            }
            let box = Utils.game.resPushNode({
                prefab: prefab,
                baseOn: "page"
            });
            box.getComponent(ColorPicker).on("color-changed", (color: cc.Color) => {
                this.colorNode.color = color;
                this.onEdit();
            }, this);

            box.setPosition(box.parent.convertToNodeSpaceAR(this.node.convertToWorldSpaceAR(cc.Vec2.ZERO)));
        }, this);
    }

    setVal(key: string, val: string) {
        this.lbKey.string = key;
        this.colorNode.color.fromHEX(val);
        this.diffNode.active = false;
    }
    setValAdditive(key: string, val: string) {
        this.lbKey.string = key;
        if (this.colorNode.color.toHEX("#rrggbbaa") == val) {
            this.colorNode.color.fromHEX(val);
            this.diffNode.active = false;
        }
        else {
            this.diffNode.active = true;
        }
    }

    onEdit() {
        this.emit("edit", this.colorNode.color.toHEX("#rrggbbaa"));
    }
};